package com.xh.demo.common.handler;

import com.xh.demo.common.consts.ViewConst;
import com.xh.demo.common.message.ICommonMessage;
import com.xh.demo.common.utils.RequestUtil;
import com.xh.demo.common.utils.WebMvcUtil;
import com.xh.demo.domain.vo.ExceptionResult;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Name CommonExceptionHandler
 * @Description
 * @Author wen
 * @Date 2019-08-01
 */
public class CommonExceptionHandler {

    /**
    * @Name result
    * @Description 公有的返回值（非ajax请求默认跳转/error路径）
    * @Author wen
    * @Date 2019/8/1
    * @param cm
    * @param request
    * @return java.lang.Object
    */
    protected Object result(ICommonMessage cm, HttpServletRequest request){
        return result(cm, null, request, ViewConst.DEFAULT_ERROR_URL);
    }

    /**
    * @Name result
    * @Description 公有的返回值（非ajax请求默认跳转/error路径）
    * @Author wen
    * @Date 2019/8/2
    * @param cm
    * @param msg
    * @param request
    * @return java.lang.Object
    */
    protected Object result(ICommonMessage cm, String msg, HttpServletRequest request){
        return result(cm, msg, request, ViewConst.DEFAULT_ERROR_URL);
    }

    /**
    * @Name result
    * @Description 公有的返回值(非ajax请求自定义跳转路径)
    * @Author wen
    * @Date 2019/8/1
    * @param cm
    * @param request
    * @param url
    * @return java.lang.Object
    */
    protected Object result(ICommonMessage cm, String msg, HttpServletRequest request, String url){
        if(StringUtils.isNotEmpty(msg)){
            cm.setMsg(msg);
        }
        if(RequestUtil.isAjaxRequest(request)){
            return ExceptionResult.build(cm);
        }
        return WebMvcUtil.cerateMAV(cm, url);
    }


}
